package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchaseBiddingPlanTwoDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchaseBiddingPlanTwo;
import com.guodu.entity.PurchasebiddingtwoStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchaseBiddingPlanTwoService;
import com.guodu.vo.PurchaseBiddingPlanTwoQueryVo;
import com.guodu.vo.PurchaseBiddingPlanTwoVo;

@Service("purchaseBiddingPlanTwoService")
public class PurchaseBiddingPlanTwoServiceImpl implements PurchaseBiddingPlanTwoService {
	
	
	@Resource
	private PurchaseBiddingPlanTwoDao purchaseBiddingPlanTwoDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;

	@Override
	public long findSizeByVo(PurchaseBiddingPlanTwoQueryVo pbpq,Units units) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwoDao.findSizeByVo(pbpq, units);
	}

	@Override
	public List<PurchaseBiddingPlanTwoVo> findByVo(
			PurchaseBiddingPlanTwoQueryVo pbpq, Units units, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<PurchaseBiddingPlanTwo> list = purchaseBiddingPlanTwoDao.findByVo(pbpq,units, curPage, pageSize);
		List<PurchaseBiddingPlanTwoVo> purchaseBiddingPlanTwoVos = new ArrayList<PurchaseBiddingPlanTwoVo>();
		for(PurchaseBiddingPlanTwo purchaseBiddingPlanTwo : list ){
			PurchaseBiddingPlanTwoVo purchaseBiddingPlanTwoVo = new PurchaseBiddingPlanTwoVo(purchaseBiddingPlanTwo);
			UserInfo userInfo = userInfoDao.findById(purchaseBiddingPlanTwo.getCreatePeople());
			Units unitsInfo = unitsDao.findById(purchaseBiddingPlanTwo.getUnitsId());
			purchaseBiddingPlanTwoVo.setUnitsName(unitsInfo.getUnitsName());
			purchaseBiddingPlanTwoVo.setIndustry(unitsInfo.getIndustry());
			Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
			purchaseBiddingPlanTwoVo.setUnitsLevel(createUnits.getUnitsLevel());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				purchaseBiddingPlanTwoVo.setFunitsName(funitsInfo.getUnitsName());
			}
			purchaseBiddingPlanTwoVo.setUserName(userInfo.getUserName());
			purchaseBiddingPlanTwoVos.add(purchaseBiddingPlanTwoVo);
		}
		return purchaseBiddingPlanTwoVos;
	}

	@Override
	public void save(PurchaseBiddingPlanTwo purchaseBiddingPlanTwo) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwoDao.save(purchaseBiddingPlanTwo);
	}

	@Override
	public PurchaseBiddingPlanTwo findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwoDao.findById(purchaseId);
	}

	@Override
	public void update(PurchaseBiddingPlanTwo purchaseBiddingPlanTwo,Long status) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwoDao.update(purchaseBiddingPlanTwo);
		List<PurchasebiddingtwoStatus> entities = new ArrayList<PurchasebiddingtwoStatus>();
		PurchasebiddingtwoStatus entity = new PurchasebiddingtwoStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchaseBiddingPlanTwo.getCreatePeople());
		entity.setPurchaseId(purchaseBiddingPlanTwo.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchaseBiddingPlanTwoDao.changeStatus(entities);
	}

	@Override
	public List<PurchasebiddingtwoStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwoDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasebiddingtwoStatus> entities = new ArrayList<PurchasebiddingtwoStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasebiddingtwoStatus entity = new PurchasebiddingtwoStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchaseBiddingPlanTwoDao.changeStatus(entities);
		purchaseBiddingPlanTwoDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwoDao.delByIds(delIds);
	}

	@Override
	public List<PurchaseBiddingPlanTwo> findAllByVo(
			PurchaseBiddingPlanTwoQueryVo pbpq, Units attribute) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwoDao.findAllByVo(pbpq,attribute);
	}

	@Override
	public PurchaseBiddingPlanTwoVo findAllMoney(
			PurchaseBiddingPlanTwoQueryVo pbpq, Units units) {
		
		return purchaseBiddingPlanTwoDao.findAllMoney(pbpq,units);
	}

	@Override
	public void examinAll(PurchaseBiddingPlanTwoQueryVo pbpq, Units units) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwoDao.examinAll(pbpq, units);
	}

}
